﻿using Furion.DependencyInjection;
using FurionApiDemo.Core.Implement.html.Dtos;
using FurionApiDemo.Util.Helper;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FurionApiDemo.Core.Implement.html
{
    public class HtmlService : IHtmlService, ITransient
    {
        private readonly ILogger<HtmlService> _logger;
        public HtmlService(ILogger<HtmlService> logger)
        {
            _logger = logger;
        }

        public async Task<List<FileItem>> GetDirectoryList()
        {
            try
            {
                return HtmlItemJson2.Instance.GetDirectoryList();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return null;
            }
        }

        public async Task<ItemFileInfo> GetItemFileInfo(string uuid)
        {
            try
            {
                return HtmlItemJson2.Instance.ItemFileInfo(uuid);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return null;
            }
        }

        public async Task<string> SaveItemFile(SaveItemFileInputDto input)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(input.UUId))
                {
                    return HtmlItemJson2.Instance.AddItemFile(input.Title, input.Url);
                }
                else
                {
                    HtmlItemJson2.Instance.UpdateItemFile(input.UUId, input.Title, input.Url, false);
                    return input.UUId;
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return "";
            }
        }

        public async Task<bool> DeleteItemFile(string uuid)
        {
            try
            {
                ItemFileInfo fileInfo = HtmlItemJson2.Instance.ItemFileInfo(uuid);
                HtmlItemJson2.Instance.UpdateItemFile(fileInfo.UUID, fileInfo.Title, fileInfo.CoverImage, true);
                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return false;
            }
        }

        public async Task<bool> RestoreItemFile(string uuid)
        {
            try
            {
                ItemFileInfo fileInfo = HtmlItemJson2.Instance.ItemFileInfo(uuid);
                HtmlItemJson2.Instance.UpdateItemFile(fileInfo.UUID, fileInfo.Title, fileInfo.CoverImage, false);
                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return false;
            }
        }

        public async Task<bool> SaveItem(SaveItemInputDto input)
        {
            try
            {
                ItemFileInfo fileInfo = HtmlItemJson2.Instance.ItemFileInfo(input.UUId);
                if (string.IsNullOrWhiteSpace(input.Id))
                {
                    //新增
                    fileInfo.Items.Add(new ItemsOutputDto(input.Url, input.Label));
                }
                else
                {
                    //修改
                    ItemsOutputDto item = fileInfo.Items.Find(t => t.Id == input.Id);
                    item.Label = input.Label;
                    item.Url = input.Url;
                }
                HtmlItemJson2.Instance.SaveItemFile(fileInfo);
                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return false;
            }
        }

        public async Task<bool> DeleteItem(DeletetemInputDto input)
        {
            try
            {
                ItemFileInfo fileInfo = HtmlItemJson2.Instance.ItemFileInfo(input.UUId);
                ItemsOutputDto item = fileInfo.Items.Find(t => t.Id == input.Id);
                fileInfo.Items.Remove(item);
                HtmlItemJson2.Instance.SaveItemFile(fileInfo);
                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return false;
            }
        }

        public async Task<bool> UpdateAllItem(UpdateAllItemInputDto input)
        {
            try
            {
                ItemFileInfo fileInfo = HtmlItemJson2.Instance.ItemFileInfo(input.UUId);
                fileInfo.Items = input.Items;
                HtmlItemJson2.Instance.SaveItemFile(fileInfo);
                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return false;
            }
        }
    }
}
